package com.picket.dao;

import java.util.Collection;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.orm.jdo.support.JdoDaoSupport;
import org.springframework.stereotype.Repository;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.picket.domain.Match;
 
@Repository
public class MatchDao extends JdoDaoSupport implements IMatchDao {
 

  
 @Override
 public void create(final List<Match> matches) {
	 for(Match match : matches){
		  getJdoTemplate().makePersistent(match);
		  System.out.println(match.getKey());
	 }

 }
 
 @Override
 public Collection<Match> getAllMatches() {
 
  final PersistenceManager pm = getPersistenceManager();
   
  try {
 
   final Query query = pm.newQuery(Match.class);
   query.setOrdering("matchNo ascending");
    
   @SuppressWarnings("unchecked")
   final Collection<Match> matches =
    (Collection<Match>) query.execute();
    
   return pm.detachCopyAll((Collection<Match>)matches);
  }
   
  finally {
   releasePersistenceManager(pm);
  }
 
 }
 
 @Override
 public Collection<Match> getAllMatchesForEvent(long eventId) {
 
  final PersistenceManager pm = getPersistenceManager();
   
  try {
 
   final Query query = pm.newQuery(Match.class);
   query.setFilter("eventId == eventIdParam");
   query.setOrdering("matchNo ascending");
   query.declareParameters("long eventIdParam"); 
   @SuppressWarnings("unchecked")
   final Collection<Match> matches =
    (Collection<Match>) query.execute(eventId);
    
   return pm.detachCopyAll((Collection<Match>)matches);
  }
   
  finally {
   releasePersistenceManager(pm);
  }
 
 }
 @Override
 public Match getMatch(long id) {
 
  final PersistenceManager pm = getPersistenceManager();
   
  try {
	  
	  Key k = KeyFactory.createKey(Match.class.getSimpleName(), id);
      return pm.getObjectById(Match.class, k);

  }
   
  finally {
   releasePersistenceManager(pm);
  }
 
 }
 @Override
	public Match updateMatch(long id, String matchResult) {
		PersistenceManager pm = getPersistenceManager();
		Match match = null;

		try {
			pm.currentTransaction().begin();
			// We don't have a reference to the selected Product.
			// So we have to look it up first,
			Key k = KeyFactory.createKey(Match.class.getSimpleName(),
					id);
			match = pm.getObjectById(Match.class, k);
			match.setMatchOutcome(matchResult);
			if(match.getMatchOutcome().equalsIgnoreCase(match.getMatchOpponent1())){
				match.setMatchOutcomePts(match.getMatchOpponentPts1());
			} else if(match.getMatchOutcome().equalsIgnoreCase(match.getMatchOpponent2())){
				match.setMatchOutcomePts(match.getMatchOpponentPts2());
			} else if(match.getMatchOutcome().equalsIgnoreCase("TIE")){
				match.setMatchOutcomePts(match.getMatchTiePts());
			} else {
				match.setMatchOutcomePts(0);
			}
			pm.makePersistent(match);
			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}
		
		return getMatch(id);
	} 
}